<?php

namespace App\Imports;

use App\Models\Car;
use App\Models\School;
use App\Models\Student;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\ToModel;

class InsureImport implements ToCollection
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Car([
            //
        ]);
    }
    public function collection(Collection $rows)
    {
        foreach ($rows as $row)
        {
            $school_id = School::query()->where('name',$row[0])->first();
            if($school_id){
                $data = [
                    'school_id'=>$school_id->id,
                    'name'    => $row[1],
                    'sex' => $row[2],
                    'phone' => $row[3],
                    'id_card' => $row[4]??'',
                    'age' => $row[5],
                    'birthday'=>$row[4]?$this->GetBirthday($row[4]):'',
                    'operator'=>$row[8],
                    'status'=>2,
                ];
                Student::query()->create($data);
            }else{
                info($row[0]);
            }
        }
    }
    private function GetBirthday($idcard){
        if(empty($idcard)) return null;
        $bir = substr($idcard,6,8);
        $year = (int)substr($bir,0,4);
        $month = (int)substr($bir,4,2);
        $day = (int)substr($bir,6,2);
        return $year.'-'.$month.'-'.$day;
    }
}
